package com.scsj.cloud.base.data.dao.impl;


import org.slf4j.Logger; 
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.scsj.cloud.base.data.dao.RedisDistributedLock;

public class RedisDistributedLockImpl implements RedisDistributedLock{
	
	
	//加锁超时时间，单位毫秒， 即：加锁时间内执行完操作，如果未完成会有并发现象 
	private static final long LOCK_TIMEOUT = 5*1000; 
	  
	private static final Logger logger = LoggerFactory.getLogger(RedisDistributedLockImpl.class); 
	  
	private StringRedisTemplate redisTemplate; 
	  
	public RedisDistributedLockImpl(StringRedisTemplate redisTemplate) {
	    this.redisTemplate = redisTemplate; 
	}

	/**
	 * 加锁，并返回上锁时间毫秒数
	 */
	@Override
	public long lock(String lockKey, String threadName) {
		Long time = 0L;
		if(redisTemplate.opsForValue().setIfAbsent(lockKey, threadName)) {
			//获得锁
			time = System.currentTimeMillis();
		}
		String redisValue = redisTemplate.opsForValue().get(lockKey);
		
		
		return 0;
	}

	@Override
	public void unlock(String lockKey, long lockValue, String threadName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public long currtTimeForRedis() {
		// TODO Auto-generated method stub
		return 0;
	}

}
